package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;


import webControlador.ControladorLista;
import beans.*;

public class Listado extends HttpServlet {

	private static final long serialVersionUID = 1L;
	ControladorLista ca ;
	
    public Listado() {
        super();
    	ca = new  ControladorLista();
    }
    private void printCebecera(PrintWriter out){
		 out.println("DOCTYPE \n" + "<HTML>\n" +
        "<HEAD><TITLE>Listado de Items</TITLE></HEAD>\n" +
        "<BODY>\n" +
        "<H1>Listado de Items</H1>\n" + 
        "<TABLE BORDER=1 ALIGN=CENTER>\n" +
        "<TR BGCOLOR=\"#FFAD00\">\n" +
        "<TD>Nombre Club</TD></TR>");	
    }
    
    private void printPie(PrintWriter out){
    	out.println("</TABLE></BODY></HTML>");	
    }

    private void printForm(PrintWriter out){

    	ControladorLista ca2 = new  ControladorLista();
    	List<String> listaMarcas = ca.buscarMarcas();
    	List<String> listaOrigenes = ca.buscarOrigenes();
    	out.println("<div style='padding:5px; border:solid 1px black'>" +
    			"<form action='/web_Servlet/Listado' method='POST'>" +
    			"Filtros");
    	
    	out.println("<select name='selectMarca'>");
    	for(String marca:listaMarcas)
    		out.println("<option value='"+marca+"'>"+marca+"</option>");
    	out.println("</select>");	
    	
    	
    	out.println("<select name='selectOrigen'>");
    	for(String origen:listaOrigenes)
    		out.println("<option value='"+origen+"'>"+origen+"</option>");
    	out.println("</select>");

    	out.println("</form>" +
    			"</div>");
    }
    
    private void printListado(HttpServletRequest request,PrintWriter out){
    	String marca = request.getParameter("selectMarca");
		String Origen =  request.getParameter("selectOrigen");
		String codigo = "";
		String descripcion = "";	
			
    	List<Object[]> listaComparativa = ca.buscarListaComparativaItems(
    			codigo, descripcion, marca, Origen);
    	
    	out.println("<table><tr>");
    	for(Object[] item:listaComparativa){
    		out.println("<td></td>");	
    	}
 
    	out.println("</tr></table>");	
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		 PrintWriter out = response.getWriter();
		 
		 printCebecera(out);
		 printForm(out);
		 printListado(request, out);
		 printPie(out);
	
		 
//		 
//		 
//		 ArrayList<String> clubes = new  ArrayList<String>();
//			try {
//				
//				Class.forName("net.sourceforge.jtds.jdbc.Driver");
//				String connectionUrl = "jdbc:jtds:sqlserver://192.168.6.100:1433;database=jugadores;";
//				con = DriverManager.getConnection(connectionUrl,usuario,password);
//				System.out.println(con.getCatalog());
//				Statement stm = con.createStatement();
//				ResultSet rs = stm.executeQuery("Select * from clubes");
//					while(rs.next())
//					{
//						clubes.add(rs.getString(2));
//					}
//			} catch (ClassNotFoundException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//			} catch (SQLException e) {
//					// TODO Auto-generated catch block
//					e.printStackTrace();
//			}
//
//		 PrintWriter out = response.getWriter();
//		 String title = "Listo Clubes";
//		 
//		 out.println("DOCTYPE \n" + "<HTML>\n" +
//	                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
//	                "<BODY>\n" +
//	                "<H1>" + title + "</H1>\n" + 
//	                "<TABLE BORDER=1 ALIGN=CENTER>\n" +
//	                "<TR BGCOLOR=\"#FFAD00\">\n" +
//	                "<TD>Nombre Club</TD></TR>");
//		 Iterator<String> i = clubes.iterator();
//		 while(i.hasNext()) {
//		 	 out.println("<TR><TD>" + i.next() + "</TD></TR>");
//		 }
//		 out.println("</TABLE></BODY></HTML>");
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		System.out.println("ssssssssssss");
//		System.out.println("asdasd");
		doGet(request, response);
	}

}
